package com.zsz.crm.settings.service.impl;

import com.zsz.crm.exception.LoginException;
import com.zsz.crm.settings.dao.UserDao;
import com.zsz.crm.settings.domain.User;
import com.zsz.crm.settings.service.UserService;
import com.zsz.crm.utils.DateTimeUtil;
import com.zsz.crm.utils.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao= SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public User login(String loginAct, String loginPwd, String ip) throws LoginException {

        Map<String,String> map=new HashMap<String, String>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);

        User user=userDao.login(map);

        if (user==null){
            throw new LoginException("账号密码错误");
        }

        //如果程序能够成功的执行到该行，说明账号密码正确
        //需要继续向下验证其他3项信息

        //验证失效时间
        String expireTime=user.getExpireTime();
        String currentTime= DateTimeUtil.getSysTime();
        if (expireTime.compareTo(currentTime) < 0){
            //账号失效
            throw new LoginException("账号已失效");
        }

        //判断锁定状态
        String lockState=user.getLockState();
        //千万别写成(lockState.equals("0"))，如果lockState是null，那么容易发生空指针异常
        if ("0".equals(lockState)){
            //账号被锁定
            throw new LoginException("账号已锁定");
        }

        //判断ip地址
        String allowIps=user.getAllowIps();
        if (!allowIps.contains(ip)){
            throw new LoginException("ip地址受限");
        }



        return user;
    }

    public List<User> getUserList() {

        List<User> uList=userDao.getUserList();
        return uList;
    }
}















































